package com.hellogood.yaohuo.server.mgr;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.log4j.Logger;

import com.google.gwt.dev.util.collect.HashMap;
import com.hellogood.yaohuo.server.DateUtil;
import com.hellogood.yaohuo.server.dao.factory.DAOFactory;
import com.hellogood.yaohuo.server.mgr.factory.MgrFactory;
import com.hellogood.yaohuo.shared.entity.Log;
import com.hellogood.yaohuo.shared.entity.QuyuOrg;
import com.hellogood.yaohuo.shared.entity.YhDetail;
import com.hellogood.yaohuo.shared.entity.YhForm;
import com.hellogood.yaohuo.shared.vo.MainFrame;
import com.hellogood.yaohuo.shared.vo.YaohuoDetailVo;

public class YaohuoMgr {
	Logger log = Logger.getLogger(this.getClass());

	public String createYaohuoForm(YhForm form) {
		String result = DAOFactory.getInstance().createYhFormDAO().createYaohuo(form, false);
		if (result.equals("")) {
			addLog(form.getYhBillNo(), false);
		}
		return result;
	}

	public String modifyYaohuoForm(YhForm form) {
		String result = DAOFactory.getInstance().createYhFormDAO().createYaohuo(form, true);
		if (result.equals("")) {
			addLog(form.getYhBillNo(), true);
		}
		return result;
	}

	private void addLog(String billNo, boolean isModify) {
		int logID = DAOFactory.getInstance().createLogDAO().getLastLogNo();
		logID++;
		Log log = new Log();
		log.setLogId(logID);
		log.setStaffCode(MainFrame.currentUser.getStaffCode());
		log.setStaffName(MainFrame.currentUser.getStaffName());
		log.setLogDate(DateUtil.currentTime());
		if (isModify) {
			log.setContent("修改要货单：" + billNo);
		} else {
			log.setContent("新建要货单：" + billNo);
		}
		DAOFactory.getInstance().createLogDAO().addNewLog(log);
	}

	public int getLastYhBillNo(String billNo) {
		String no = DAOFactory.getInstance().createYhFormDAO().getLastYhBillNo(billNo);
		if (no != null) {
			// 序号为最后两位 格式为"WLYH组织号YYYYMMDDXX" XX为序号
			if (no.length() > 2) {
				String lastNo = no.substring(no.length() - 2, no.length());
				return Integer.valueOf(lastNo);
			}
		}
		return 0;
	}

	public List<YhForm> searchYhForm(String code, int level, String startDate, String endDate, String rz) {
		if (code.equals("美季")) {
			level = 1;
		}
		if (level == 2) {
			return DAOFactory.getInstance().createYhFormDAO().searchYhFormByQuyu(code, startDate, endDate, rz);
		} else if (level == 3) {
			return DAOFactory.getInstance().createYhFormDAO().searchYhForm(code, startDate, endDate, rz);
		} else if (level == 1) {
			return DAOFactory.getInstance().createYhFormDAO().searchYhFormByQuyu(null, startDate, endDate, rz);
		}
		return null;
	}

	public YhForm getFormDetail(String billNo) {
		YhForm form = DAOFactory.getInstance().createYhFormDAO().getFormByBillNo(billNo);
		if (form != null) {
			List<YhDetail> list = DAOFactory.getInstance().createYhDetailDAO().getDetailByBillNo(billNo);
			List<YaohuoDetailVo> voList = new ArrayList<YaohuoDetailVo>();
			Map<String, List<YhDetail>> map = new HashMap<String, List<YhDetail>>();
			for (int i = 0; i < list.size(); i++) {
				YhDetail d = list.get(i);
				String key = d.getBarCode() + "-" + d.getBra() + "-" + d.getColor();
				List<YhDetail> temp = map.get(key);
				if (temp == null) {
					temp = new ArrayList<YhDetail>();
				}
				temp.add(d);
				map.put(key, temp);
			}
			Set<String> set = map.keySet();
			Iterator<String> it = set.iterator();
			while (it.hasNext()) {
				String key = it.next();
				List<YhDetail> temp = map.get(key);
				YaohuoDetailVo vo = new YaohuoDetailVo();
				int counts = 0;
				for (int i = 0; i < temp.size(); i++) {
					YhDetail d = temp.get(i);
					if (i == 0) {
						vo.setBarCode(d.getBarCode());
						vo.setBra(d.getBra());
						vo.setColor(d.getColor());
						vo.setPluCode(d.getPluCode());
						vo.setPluName(d.getPluName());
						vo.setRemark(d.getRemark());
						vo.setOrgCode(d.getOrgCode());
					}
					if (d.getSize().equals("1")) {
						vo.setSize1(d.getCounts());
						counts += d.getCounts();
					} else if (d.getSize().equals("2")) {
						vo.setSize2(d.getCounts());
						counts += d.getCounts();
					} else if (d.getSize().equals("3")) {
						vo.setSize3(d.getCounts());
						counts += d.getCounts();
					} else if (d.getSize().equals("4")) {
						vo.setSize4(d.getCounts());
						counts += d.getCounts();
					} else if (d.getSize().equals("5")) {
						vo.setSize5(d.getCounts());
						counts += d.getCounts();
					} else if (d.getSize().equals("6")) {
						vo.setSize6(d.getCounts());
						counts += d.getCounts();
					} else if (d.getSize().equals("0") || d.getSize().equals("7")) {
						vo.setSize(d.getCounts());
						counts += d.getCounts();
					}
				}
				vo.setCounts(counts);
				voList.add(vo);
			}
			form.setVoList(voList);
		}
		return form;
	}

	public List<YhDetail> search(String quyuCode, String date) {
		List<QuyuOrg> list = MgrFactory.getInstance().createQuyuOrgMgr().getQuyuOrgByCode(quyuCode);
		String code = "";
		for (QuyuOrg q : list) {
			code += "," + q.getOrgCode();
		}

		if (code.substring(0, 1).equals(",")) {
			code = code.substring(1, code.length());
		}

		return DAOFactory.getInstance().createYhDetailDAO().searchDetail(code, date);
	}

	public String deleteYaohuo(String billNo) {
		return DAOFactory.getInstance().createYhFormDAO().deleteYaohuo(billNo);
	}

	public List<YhDetail> getYaohuohuizong(String quyuCode, String date) {
		List<YhDetail> list = new ArrayList<YhDetail>();
		list = DAOFactory.getInstance().createYhDetailDAO().getYaohuohuizong(quyuCode, date);
		return list;
	}

	public String rz(String billNo) {
		return DAOFactory.getInstance().createYhFormDAO().rz(billNo);
	}

	public String rz(List<String> bills) {
		String result = "";
		for (String bill : bills) {
			String temp = DAOFactory.getInstance().createYhFormDAO().rz(bill);
			if (!temp.equals("")) {
				result = temp;
			}
		}
		return result;
	}
}
